package LeetCode.tree;

import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class TestOrder {

    //中序遍历
    public static void inoder(BinSearchTree root){
         Stack<BinSearchTree> stack = new Stack<BinSearchTree>();
         BinSearchTree node = root;
         while(node != null || !stack.isEmpty()){
             while(node != null){
                 stack.push(node);
                 node = node.left;
             }
             if(!stack.isEmpty()){
                 node = stack.pop();
                 System.out.println(node.data);
                 node = node.right;
             }
         }
    }

    //前序遍历
    public static  void preorder(BinSearchTree root){
        Stack<BinSearchTree> stack = new Stack<BinSearchTree>();
        BinSearchTree node = root;
        while(node != null || !stack.isEmpty()){
            while(node != null){
                 stack.push(node);
                System.out.println(node.data);
                node = node.left;
            }
            if(!stack.isEmpty()){
                node = stack.pop();
                node = node.right;
            }
        }
    }

    public static void main(String[] args) {
        BinSearchTree node = BinSearchTree.createBintree();
        //TestOrder.inoder(node);
        TestOrder.preorder(node);
    }
}
